package com.zjdiepu.www.biz.other;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.other.OtherWarningConfigurationMapper;
import com.zjdiepu.www.dao.system.BaseSalersmanMapper;
import com.zjdiepu.www.ibiz.other.IOtherWarningConfigurationService;
import com.zjdiepu.www.model.other.dto.OtherWarningConfiguration;
import com.zjdiepu.www.model.other.ext.OtherWarningConfigurationExt;
import com.zjdiepu.www.model.system.dto.BaseSalersman;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * Service层操作实现<br/>
 * @author zhangpp<br/>
 * @time 2017年06月22日 17时59分<br/>
 * @since 1.0<br/>
 */
@Service
public class OtherWarningConfigurationServiceImpl extends BaseService implements IOtherWarningConfigurationService {
	public static final Logger logger = LoggerFactory.getLogger(OtherWarningConfigurationServiceImpl.class);

	@Autowired
	private OtherWarningConfigurationMapper otherWarningConfigurationMapper;
	@Autowired
	private BaseSalersmanMapper baseSalersmanMapper;
	
	/**
	 *  获取预警时间配置:根据业务员ID
	 * @author xiongxiaotun <br/>
	 * @Title: getOtherWarningConfigurationBySalesmanId  <br/>
	 * @Description:  获取预警时间配置:根据业务员ID <br/>
	 * @param warningType 预警类型
	 * @param salesmanId
	 * @return
	 * @version V1.0  <br/>
	 */
	@Override
	public OtherWarningConfiguration getOtherWarningConfigurationBySalesmanId(String warningType, String salesmanId){
		if(StringUtils.isBlank(salesmanId)) {
			ExceptionHelper.thow("999", "业务员ID为空");
		}
		BaseSalersman baseSalersman = baseSalersmanMapper.selectByPrimaryKey(salesmanId);
		if(null == baseSalersman) {
			ExceptionHelper.thow("999", "业务员记录丢失");
		}
		String unitCode = baseSalersman.getUnitCode();
		
		return getOtherWarningConfigurationByUnitCode(warningType, unitCode);
	}
	
	/**
	 * 获取预警时间配置:根据组织编码
	 * @author xiongxiaotun <br/>
	 * @Title: getOtherWarningConfigurationByUnitCode  <br/>
	 * @Description: 获取预警时间配置  <br/>
	 * @param warningType 预警类型
	 * @param unitCode
	 * @return
	 * @version V1.0  <br/>
	 */
	@Override
	public OtherWarningConfiguration getOtherWarningConfigurationByUnitCode(String warningType, String unitCode)	{
		if(StringUtils.isBlank(unitCode)) {
			ExceptionHelper.thow("999", "业务机构编码丢失");;
		}
		
		if(unitCode.length() < 2) {
			ExceptionHelper.thow("999", "业务机构编码不符合规则");
		}
		
		OtherWarningConfigurationExt otherWarningConfigurationExt = new OtherWarningConfigurationExt();
		otherWarningConfigurationExt.setBaseWhere();
		otherWarningConfigurationExt.setRows(1);
		otherWarningConfigurationExt.setWarningType(warningType);
		otherWarningConfigurationExt.setBusiUnitCode(unitCode);
		List<OtherWarningConfiguration> otherWarningConfigurations = otherWarningConfigurationMapper.selectByExt(otherWarningConfigurationExt);
		if(CollectionUtils.isEmpty(otherWarningConfigurations)) {
			if(unitCode.length() < 4) {
				return null;
			} else {
				String finalUnitCode = null;
				if(unitCode.length() == 4) { //4
					finalUnitCode = unitCode.substring(0, unitCode.length() - 2);
				} else { // > 4
					finalUnitCode = unitCode.substring(0, unitCode.length() - 3);
				}
				return getOtherWarningConfigurationByUnitCode(warningType, finalUnitCode);
			}
		} else {
			return otherWarningConfigurations.get(0);
		}
	}
	
	@Override
	public void saveUpdateAndDelete(OtherWarningConfiguration otherWarningConfiguration, OperateType operType) {
		switch (operType) {
		case add:
			otherWarningConfiguration.initInsertData(otherWarningConfiguration.getModifier(), otherWarningConfiguration.getModifierName());
			otherWarningConfigurationMapper.insert(otherWarningConfiguration);
			break;
		case edit:
			otherWarningConfiguration.initUpdateData(otherWarningConfiguration.getModifier(), otherWarningConfiguration.getModifierName());
			// 影响的记录数
			int updateCount = otherWarningConfigurationMapper.updateByPrimaryKeySelective(otherWarningConfiguration);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			otherWarningConfiguration.initDeleteData(otherWarningConfiguration.getModifier(), otherWarningConfiguration.getModifierName());
			// 影响的记录数
			int deleteCount = otherWarningConfigurationMapper.updateByPrimaryKeySelective(otherWarningConfiguration);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<OtherWarningConfiguration> selectByExt(OtherWarningConfigurationExt otherWarningConfigurationExt) {
		return otherWarningConfigurationMapper.selectByExt(otherWarningConfigurationExt);
	}
	
	@Override
	public int countByExt(OtherWarningConfigurationExt otherWarningConfigurationExt){
		return otherWarningConfigurationMapper.countByExt(otherWarningConfigurationExt);
	}
	
	@Override
	public OtherWarningConfiguration selectById(String id) {
		return otherWarningConfigurationMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			otherWarningConfigurationMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(OtherWarningConfiguration otherWarningConfiguration) {
		return otherWarningConfigurationMapper.countRefined(otherWarningConfiguration);
	}

	@Override
	public OtherWarningConfiguration selectRefined(OtherWarningConfiguration otherWarningConfiguration) {
		return otherWarningConfigurationMapper.selectRefined(otherWarningConfiguration);
	}
	
	@Override
	public int deletedRefined(OtherWarningConfiguration otherWarningConfiguration) {
		return otherWarningConfigurationMapper.deletedRefined(otherWarningConfiguration);
	}

	@Override
	public boolean repeat(OtherWarningConfiguration otherWarningConfiguration) {
		int count = countRefined(otherWarningConfiguration);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(otherWarningConfiguration.getId())) {
			return true;
		}
		OtherWarningConfiguration dbItem = selectRefined(otherWarningConfiguration);
		if(null == dbItem) {
			return false;
		}
		return !otherWarningConfiguration.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return otherWarningConfigurationMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<OtherWarningConfiguration> otherWarningConfigurations) {
		return otherWarningConfigurationMapper.insertBatch(otherWarningConfigurations);
	}

	@Override
	public int countMulitByExt(
			OtherWarningConfigurationExt otherWarningConfigurationExt) {
		return otherWarningConfigurationMapper.countMulitByExt(otherWarningConfigurationExt);
	}

	@Override
	public List<OtherWarningConfiguration> selectMulitByExt(
			OtherWarningConfigurationExt otherWarningConfigurationExt) {
		return otherWarningConfigurationMapper.selectMulitByExt(otherWarningConfigurationExt);
	}
}
